using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Controls;
using MindFactorial.LaunchPoint.Common;

namespace MindFactorial.LaunchPoint.Console.Library
{
    /// <summary>
    /// Represents the presentation surface for menus
    /// </summary>
    public class MenuContextPresenter : LaunchPointObject 
    {
        List<MenuItem> _items = new List<MenuItem>();
        Dictionary<string, MenuContextPresenter> _menus = new Dictionary<string, MenuContextPresenter>();
        MenuItem _menuItem;

        internal MenuContextPresenter(MenuItem item)
        {
            this._menuItem = item;
        }

        /// <summary>
        /// Gets the internal menu item this context encapsulates
        /// </summary>
        public MenuItem InternalItem
        {
            get
            {
                return _menuItem;
            }

        }


        /// <summary>
        /// Adds all menus in the ItemCollection to the current context presenter
        /// </summary>
        /// <param name="menuitems"></param>
        public void SetItems(ItemCollection menuitems)
        {
            foreach (MenuItem menuitem in menuitems)
            {
                _items.Add(menuitem);
            }

            //add these to appropriate menu
            foreach (MenuItem menuitem in _items)
            {
                //check to see if the menu's parent item is null first
                if (menuitem.Parent != null)
                {
                    if (menuitem.Parent is Menu)
                    {
                        Menu menu = (Menu)menuitem.Parent;
                        menu.Items.Remove(menuitem);
                    }
                    else if (menuitem.Parent is MenuItem)
                    {
                        MenuItem menu = (MenuItem)menuitem.Parent;
                        menuitem.Items.Remove(menuitem);
                    }
                    else
                    {
                        MindFactorial.LaunchPoint.Runtime.PromptManager.Alert("8869F2FC-3F04-47c3-9E8D-DF47199261B9: Subchild of non memu");
                    }
                }
                
                _menuItem.Items.Add(menuitem);
            }
        }

        /// <summary>
        /// Sets menu items
        /// </summary>
        /// <param name="menu"></param>
        public void SetItems(Menu menu)
        {
            //if(menu.Tag == null)
            //    menu.Tag = menu.Items;

            SetItems(menu.Items);
        }

        /// <summary>
        /// Removes the preloaded list of items from the list
        /// </summary>
        public void ClearItems()
        {
            //save items to queue before removing from item list
            Queue<MenuItem> items = new Queue<MenuItem>(_items);

            //remove from menu
            foreach (MenuItem menuitem in _items)
            {
                _menuItem.Visibility = System.Windows.Visibility.Hidden;
                //_menuItem.Items.Remove(menuitem);
            }

            //remove item from list of items

            foreach (MenuItem item in items)
            {
               // _items.Remove(item);
            }

            
        }

        /// <summary>
        /// Iterate through child items of this presentation context
        /// </summary>
        /// <param name="menuHeader"></param>
        /// <returns></returns>
        public MenuContextPresenter this[string menuHeader]
        {
            get
            {

                if (!_menus.ContainsKey(menuHeader))
                {
                    foreach (MenuItem item in _menuItem.Items)
                    {

                        if (item.Tag.ToString() == menuHeader)
                        {
                            MenuContextPresenter presenter = new MenuContextPresenter(item);
                            _menus.Add(menuHeader, presenter);
                            return presenter;
                        }
                    }
                }
                else
                    return _menus[menuHeader];


                return null;

            }
        }

    }
}
